package com.gxa.community.admin.service.analysis.impl;

import com.gxa.community.admin.mapper.analysis.OperationMonitoringMapper;
import com.gxa.community.admin.service.analysis.OperationMonitoringService;
import com.gxa.community.core.pojo.domain.OrderInfo;
import com.gxa.community.core.pojo.dto.analysis.OperationMonitoringDTO;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class OperationMonitoringServiceImpl implements OperationMonitoringService {

    @Autowired
    private OperationMonitoringMapper operationMonitoringMapper;

    @Override
    public R queryOperationAnalysisData() {
        OperationMonitoringDTO operationMonitoringDTO = new OperationMonitoringDTO();

        //当日付款订单数量,先对时间做处理
        long currentTime = System.currentTimeMillis();
        //当天0点
        long zero = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        List<OrderInfo> orderInfos = operationMonitoringMapper.queryCurrentDayOrderPayed(zero, currentTime);
        operationMonitoringDTO.setOrderPayedNum(orderInfos.size());
        //当日付款订单总金额、客单价
        Set<Integer> set = new HashSet<>();
        BigDecimal total=null;
        for (OrderInfo orderInfo : orderInfos) {
            total = total.add(orderInfo.getPayment());
            set.add(orderInfo.getBuyerId());
        }
        operationMonitoringDTO.setPaymentAmount(total);
        //TODO 用户活跃数，转化率，

//        operationMonitoringDTO.setActiveUsers();
//        operationMonitoringDTO.setConversionRate();
        //客单价
        operationMonitoringDTO.setCustomerUnitPrice(total.divide(new BigDecimal(set.size())));
        //新增用户
        operationMonitoringDTO.setNewUsers(operationMonitoringMapper.queryCurrentUsersAdd(zero, currentTime));
        //待付款订单数
        operationMonitoringDTO.setWaitPayOrderNum(operationMonitoringMapper.queryWaitPayOrderNum());
        //待发货社区数量
        operationMonitoringDTO.setWaitSendNum(operationMonitoringMapper.queryWaitSendNum());
        //TODO 团长申请审核 status = 0
        operationMonitoringDTO.setAgentApplyWaitToExamineNum(operationMonitoringMapper.queryAgentApplyWaitToExamineNum());
        //售后订单待审核
        operationMonitoringDTO.setAfterSaleOrderWaitToExamineNum(operationMonitoringMapper.queryAfterSaleOrderWaitToExamineNum());
        //商品库存不足商品数(库存低于10吧)
        operationMonitoringDTO.setInsufficientInventoryOfGoodsNum(operationMonitoringMapper.queryInsufficientInventoryOfGoodsNum());
        //广告即将到期广告数(结束时间距离当前时间不足1天)
        long date = 1000*60*60*24;//1天的毫秒数
        long date1 = currentTime+date;
        operationMonitoringDTO.setAdMayExpireNum(operationMonitoringMapper.queryAdMayExpireNum(date1));
        //订单统计:订单总数（默认本月）(已支付）、订单统计同比

//        operationMonitoringDTO.setOrderStatisticsData();
        //交易金额统计（默认本月）、交易金额统计同比

//        operationMonitoringDTO.setTransactionAmountStatisticsData();
        //商品销量排行（默认本月）

//        operationMonitoringDTO.setProductSaleRanking();
        //团长销售排行（默认本月）

//        operationMonitoringDTO.setAgentSaleRanking();

        return R.ok("查询成功！").put("data",operationMonitoringDTO);
    }
}
